package com.chuzhiyun.chengguoxy.utlis;

import android.content.Context;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.amap.api.services.core.ServiceSettings;

public class LocationUtils  {
    static LocationUtils mLocationUtils = null;
    AMapLocationClientOption mLocationOption;

    private LocationUtils() {
        initOption();
    }

    public static LocationUtils getInstance() {
        if (mLocationUtils == null) {
            synchronized (LocationUtils.class) {
                if (mLocationUtils == null) {
                    mLocationUtils = new LocationUtils();
                }
            }
        }
        return mLocationUtils;
    }

    public void getLocation(Context myContent, OnLocationChangedListener listener) throws Exception {
        mListener = listener;
        AMapLocationClient.updatePrivacyShow(myContent,true,true);
        AMapLocationClient.updatePrivacyAgree(myContent,true);
        init(myContent);
    }
    private void initOption(){

        //初始化定位参数
        AMapLocationClientOption mLocationOption = new AMapLocationClientOption()
                .setNeedAddress(true)//设置是否返回地址信息（默认返回地址信息）
                .setLocationMode(AMapLocationClientOption.
                        AMapLocationMode.Hight_Accuracy)
                .setOnceLocation(true);

    }
    private void init(Context myContent) throws Exception {
        AMapLocationClient myLocationClient = new AMapLocationClient(myContent);
        myLocationClient.setLocationListener(new AMapLocationListener() {
            @Override
            public void onLocationChanged(AMapLocation amapLocation) {
                if (amapLocation != null) {
                    if (amapLocation.getErrorCode() == 0) {
                        //定位成功回调信息，设置相关消息
                        amapLocation.getLocationType();//获取当前定位结果来源，如网络定位结果，详见定位类型表
                        if (mListener != null) {
                            if (!StringUtils.isEmpty(amapLocation.getAdCode())){
                                mListener.onSuccess(amapLocation);
                                myLocationClient.stopLocation();
                            }
                        }

                    } else {
                        if (mListener != null) {
                            mListener.onFail(amapLocation.getErrorCode(), amapLocation.getErrorInfo());
                            myLocationClient.stopLocation();
                        }
                    }
                }
            }
        });

        //设置定位参数
        myLocationClient.setLocationOption(mLocationOption);
        myLocationClient.startLocation();
    }

    OnLocationChangedListener mListener;

    public interface OnLocationChangedListener {
        /**/
        void onSuccess(AMapLocation amapLocation);

        /**
         * 失败
         *
         * @param errCode 错误码
         * @param errInfo 错误信息
         */
        void onFail(int errCode, String errInfo);
    }

}